﻿/*******************************************************************
 *  Copyright(c) 2018-2018 A single spark
 *  All rights reserved.
 *
 *  文件名称:yuvdispalywidget.cpp
 *  简要描述:播放界面图像绘制以及控制接口
 *
 *  修改日期:  2018.03.20
 *  作者:      LD
 *  邮箱:
 *  说明:

 ******************************************************************/
#include "yuvdispalywidget.h"
#include "yuvoperater.h"

#include <QPainter>

YUVDispalyWidget::YUVDispalyWidget(QWidget *parent)
    : YUVWidget(parent),
      m_imgVideo(QImage()),
      m_pYuvOperater(Q_NULLPTR)
{
    m_pYuvOperater = new yuvoperater;
    m_pYuvOperater->Init("d:\\sintel_480x272_yuv420p.yuv", AV_PIX_FMT_YUV420P, 480,272);
    connect(m_pYuvOperater, SIGNAL(sigOnFrame(QImage, quint64, quint64)),
            this, SLOT(slotOnOneFrame(QImage, quint64, quint64)));
}

YUVDispalyWidget::~YUVDispalyWidget()
{

}

void YUVDispalyWidget::init(QString strFile)
{
    m_pYuvOperater->Init(strFile, AV_PIX_FMT_YUV420P, 480,272);
}

void YUVDispalyWidget::play()
{
    m_pYuvOperater->play();
}

void YUVDispalyWidget::pause()
{
    m_pYuvOperater->pusePlay();
}

void YUVDispalyWidget::stop()
{
    m_pYuvOperater->stopPlay();
}

void YUVDispalyWidget::prevFrame()
{
    m_pYuvOperater->preFrame();
}

void YUVDispalyWidget::nextFrame()
{
    m_pYuvOperater->nextFrame();
}

void YUVDispalyWidget::paintEvent(QPaintEvent *event)
{
    YUVWidget::paintEvent(event);
    if (m_imgVideo.isNull())
    {
        return;
    }

    QPainter painter(this);

    if (m_imgVideo.size().width() <= 0) return;
    ///将图像按比例缩放成和窗口一样大小
    QImage img = m_imgVideo.scaled(this->size(),Qt::IgnoreAspectRatio);
    int x = this->width() - img.width();
    int y = this->height() - img.height();
    painter.drawImage(QPoint(x,y),img);
}

void YUVDispalyWidget::slotOnOneFrame(QImage img, quint64 nCurFrameIndex, quint64 nTotalFrames)
{
    m_imgVideo = img;
    update();

    emit sigOnFrame(nCurFrameIndex, nTotalFrames);
}
